<?php
/**
 * Introduction: 用户等级表  tarento
 */

namespace Common\Model;


use Think\Model;

class TarentoModel extends Model
{
    //定义数据库连接信息
    protected $connection;

    //定义表名
    protected $tableName = 'tarento';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }

    /**
     * 获取一条信息
     */
    public function getRow($member_id){
        $info = $this->where("member_id=$member_id")->find();
        return $info;
    }

    /**
     * 获取用户加密后的支付密码
     *
     * @param unknown $pwd
     */
    public function getPayPwdEncode($pwd)
    {
        $pwdEncode = substr(hash('sha512', self::PAY_PWD_KEY . $pwd), 0, 32);
        return $pwdEncode;
    }

    /**
     * 验证支付是否正确
     */
    public function verification_pay_pwd($member_id,$pwd){
        return $this->where("member_id=$member_id and pay_password='$pwd'")->find();
    }

    /**
     * 获取分店列表
     * @param unknown $relations
     * @param unknown $subbranchCountRelation
     * @param unknown $currentPage
     * @param unknown $pageSize
     * @param unknown $isNeedTotal
     */
    public function getChildShopsIdByLevel($member_id,$num,$limit)
    {
        $list = $this->alias('t')->join("left join haiou_micro_shop as m on t.member_id=m.member_id left join haiou_member as mb on m.member_id=mb.userid")->where("t.direct_rec_mid=$member_id and m.status>0 and m.level=3")->field("m.member_id,m.name,m.logo,m.level,mb.name as nick_name")->limit($num,$limit)->order("m.create_time desc")->select();
        foreach($list as $k=>$v){
            //吃货下面的吃货个数
            $num = $this->alias('t')->join("left join haiou_micro_shop as m on t.member_id=m.member_id")->where("t.direct_rec_mid=".$v['member_id']." and m.status>0 and m.level=3")->count();
            $model = new OrderModel();
            //计算订单量
            $order_list = $model->where("member_id=".$v['member_id']." and separate_status<=0 and status>=2")->select();
            $total_price = 0;
            foreach($order_list as $s){
                $total_price+=$s['cost'];
            }
            $list[$k]['order_count'] = count($order_list);
            $list[$k]['num'] = $num;
            $list[$k]['total_price'] = $total_price;
        }
        $res['total_num'] = count($list);
        $res['list'] = $list;
        return $res;
    }

    /**
     * 构建用户邀请码
     *
     * @param unknown $userId
     */
    public function buildInviteCode($userId)
    {
        // TODO:暂时随机生成
        $codeArr = array();
        for ($i = 0; $i < 4; $i ++) {
            while (true) {
                $ascii = mt_rand(65, 90);
                if ($ascii != 79 && $ascii != 73) {
                    // 排除O字母和I
                    break;
                }
            }
            $codeArr[] = chr($ascii);
        }
        $code = implode('', $codeArr);

        //不开放只有2个字母和ABCD号
        $isOk = false;
        $countArr = array_count_values($codeArr);
        if (count($countArr)<=2 || $code == 'ABCD') {
            // 踩到靓号了，则递归重新生成
            return $this->buildInviteCode($userId);
        }
        $existsData = $this->where("invite_code='$code'")->field("id")->find();
        if (! empty($existsData)) {
            // 重复了，则递归生成
            return $this->buildInviteCode($userId);
        }
        return $code;
    }

    /**
     * 获取平级店铺列表
     * @param unknown $memberId
     * @param unknown $level
     * @param unknown $currentPage
     * @param unknown $pageSize
     * @param unknown $isNeedTotal
     * @return number[]
     */
    public function getRecEqualLevelList($memberId, $level, $num, $limit)
    {

        $list = $this->alias('a')->join("left join haiou_micro_shop as b on a.member_id=b.member_id")
            ->where("a.direct_rec_mid=$memberId and b.level=$level and b.status>0")
            ->field("a.member_id")->group("a.member_id")->order("a.create_time desc")->limit($num,$limit)->select();

        $res = ['shopList' => $list];
        return $res;
    }

    /**
     * 统计商圈消费者人数  首席 红人  吃货
     * @param string $relations
     * @param boolean $isOnlyCustomer
     * @param string $dateStart
     * @param string $dateEnd
     */
    public function countCustomerNumbers($memberId ,$level)
    {
        return $this->where("direct_rec_mid=$memberId and level=$level")->count();
    }

}